{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 24 自定义函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.【选择】请选出以下代码的运行结果（ ）  \n",
    "`\n",
    "def InputInt( a ):  \n",
    "    a = 15  \n",
    "b = 2  \n",
    "InputInt(b)  \n",
    "print( b )`\n",
    "\n",
    "A.15  \n",
    "B.2  \n",
    "C.报错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.【填空】请填出横线处空缺的一行，以便运行得出正确的结果（ ）  \n",
    "\n",
    "`def func(k):\n",
    "    _____\n",
    "    k+=5\n",
    "    j=k\n",
    "    print(j)\n",
    "j=1    \n",
    "func(2)   \n",
    "print(j)`\n",
    "\n",
    "输出为：  \n",
    "7  \n",
    "7  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：global j"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.【改错】以下代码有误，导致无法正确运行得出结果，请写出正确的代码（ ）  \n",
    "`def func(j=5,i):\n",
    "    i+=1  \n",
    "d=func(2,4)\n",
    "print(d) `  \n",
    "\n",
    "输出为：  \n",
    "(3, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：  \n",
    "`def func(i,j=5):\n",
    "    i+=1\n",
    "    return i,j   \n",
    "d=func(2,4)\n",
    "print(d)`    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "4.【编程】写函数，计算长是8，宽是6的矩形面积。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：   \n",
    "`\n",
    "def area(width, height):\n",
    "    return width * height\n",
    "w = 6\n",
    "h = 8\n",
    "print(\"width =\", w, \" height =\", h, \" area =\", area(w, h))`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.【编程】写函数，利用递归获取斐波那契数列中的第6个数，并将该值返回给调用者。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：  \n",
    "`\n",
    "def func(x,y,count):\n",
    "    count +=1\n",
    "    z = x + y\n",
    "    if count ==6:\n",
    "        print(x)\n",
    "        return\n",
    "    func(y,z,count)\n",
    "func(0,1,0)`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.【编程】写函数，利用递归方法求10!。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：  \n",
    "`def comp(x):\n",
    "    sum=0\n",
    "    if x == 1:\n",
    "        sum=1\n",
    "    else:\n",
    "        sum=x*comp(x-1)\n",
    "    return sum\n",
    "print (comp(10))`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.【编程】编写函数，使输入一个数字n，如果n是偶数，则调用函数求1/2+1/4+...+1/n，如果n是奇数，则调用函数求1+1/3+1/5+...+1/n。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "答案：  \n",
    "`\n",
    "def peven(n):\n",
    "    i=0\n",
    "    s=0\n",
    "    for i in range (2,n+1,2):\n",
    "        s+=1/i\n",
    "    return s\n",
    "def podd(n):\n",
    "    i=0\n",
    "    s=0\n",
    "    for i in range (1,n+1,2):\n",
    "        s+=1/i\n",
    "    return s\n",
    "n=int(input(\"请输入一个数字\"))\n",
    "if n % 2 == 0:\n",
    "    sum=peven(n)\n",
    "else:\n",
    "     sum=podd(n)\n",
    "print(sum)`"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
